var e = getApp(), a = e.requirejs("core"), t = e.requirejs("wxParse/wxParse"), i = e.requirejs("biz/diypage"), s = e.requirejs("jquery");


Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 楼主信息
   commentHeaderList:[],
  //  内容图片
   imageList:[],
   boardList:[],
  // 评论信息
  repList:[],
  // @相关信息
  parentList:[],
  id:'',
  isAuthor:true,
  isgood:0,
  good:'',

  },

  QueryParams:{
    bid:'',
    pid:'',
    page:''
  },
pid:'',
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options)
    this.QueryParams.pid = options.id
   this.getcomment()
  },
  onShow:function(){
    // console.log(this.data.cid);
    let bid=wx.getStorageSync('bid')
    // console.log(bid);
    this.setData({
      id:bid
    })
    this.getreply(this.data.id,this.QueryParams.pid)
   this.getcomment()

    // console.log(this.data.id);
  },
   // 获取头部信息
   getcomment(){
    a.get('sns/board_postdetail&bid=&pid=&page=1',this.QueryParams,(res) => {
      // console.log(res)
      let commentHeaderList =res.result.board
      let imageList=res.result.post.images
      let boardList=res.result.post
  
      // console.log(commentHeaderList)
      // console.log(imageList)
      // console.log(boardList.bid)

      this.setData({
        commentHeaderList,
        imageList,
        boardList   
      })
    })
  },
  // 获取评论数据
  getreply(id,pid){
    a.get('sns/board_postdetail&bid='+id+'&pid='+pid+'&page=1',[this.QueryParams,id],(res) => {
      // console.log(res)
      let repList=res.result.list
     if(this.data.parent===true){
      let parentList=repList.parent
        this.setData({
          parentList
        })
     }else{
      let parentList=[]
      this.setData({
        parentList
      })
     }
      // console.log(id)
      this.setData({
        repList
      })
    })
  },
// 回复
  handle(){
    wx.setStorageSync('bid', this.data.id)
    this.pid=this.QueryParams.pid
    // console.log( this.pid);
    wx.setStorageSync('pid', this.pid)
  },
  //  点赞功能
favorclick(e){
  // console.log(e);
  let bid = wx.getStorageSync('bid')
  let pid = e.currentTarget.dataset.id
  let isgood = this.data.isgood
  wx.setStorageSync('pid', pid)
  this.setData({
    bid,
    pid,
    isgood,
  })
  this.getgood(this.data.bid,this.data.pid,this.data.isgood)
  
},
// 点赞请求
  getgood(bid,pid,isgood){
    a.post('sns/board_like',{bid,pid,isgood},(res) => {
      // console.log(pid);
      let isgood =res.result.isgood
      // console.log(isgood);
      
      if(res.status==1){
          let boardList = this.data.boardList
            if(boardList.id==pid){
                  if(isgood==1){
                    boardList.goodcount=boardList.goodcount*1+1
                  }else{
                    boardList.goodcount=boardList.goodcount*1-1
                }
              }
              let pages = getCurrentPages()
              let page = pages[pages.length-1]
              // console.log(page);
              
              let prepage = pages[pages.length-2]
              let contentList = prepage.data.contentList
              contentList= contentList.map(v=>{
                if(v.id==pid){
                 v.goodcount=page.data.boardList.goodcount
                }
                return v
              })
              // console.log(contentList);
              this.setData({
                boardList
              })
              prepage.setData({
                contentList
              })
        //  console.log(this.data.boardList);
        }
    })
   
  }
})